/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eng.scc.core.panes;

import com.eng.scc.core.data.DeviceInfo;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.parts.DeviceInfoTable;
import com.eng.scc.core.view.ApplicationContext;
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author Rafael
 */
public class DeviceDataPane extends javax.swing.JPanel
{
    private int deviceId;

    /**
     * Creates new form DeviceDataPane
     */
    public DeviceDataPane()
    {
        initComponents();
    }

    public void setDeviceID( int deviceId )
    {
        try
        {
            this.deviceId = deviceId;
            
            Date dateEnd = new Date();
            
            Calendar c = Calendar.getInstance();
            c.add( Calendar.DAY_OF_MONTH, -1 );
            
            updateDeviceData( c.getTime(), dateEnd, this.deviceId );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        deviceInfoTable = new DeviceInfoTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setViewportView(deviceInfoTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jScrollPane1, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Filtro"));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("De:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(firstDateTime, gridBagConstraints);

        jLabel2.setText("Até:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lastDateTime, gridBagConstraints);
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.NONE;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
//        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(btRefresh, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jPanel1, gridBagConstraints);
        
        btRefresh.addActionListener( new ActionListener()
        {

            @Override
            public void actionPerformed( ActionEvent e )
            {
                updateDeviceData( firstDateTime.getDate(), lastDateTime.getDate() , deviceId );
            }
        });
    }

    private void updateDeviceData( Date dateStart, Date dateEnd, int deviceId )
    {
        try
        {
            List<DeviceInfo> deviceInfos = FuelControllerManagerService.getInstance().getDeviceInfos( new Timestamp( dateStart.getTime() ), 
                                                                                                      new Timestamp( dateEnd.getTime() ) , 
                                                                                                      deviceId );

            if ( deviceInfos != null )
            {
                deviceInfoTable.setDeviceInfos( deviceInfos );
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    static class DateTimePicker extends JPanel
    {

        public DateTimePicker()
        {
            initComponents();
        }
        
        public Date getDate()
        {
            Calendar c = Calendar.getInstance();
            c.setTime( date.getDate() );

            Calendar c1 = Calendar.getInstance();
            c1.setTime( (Date) time.getValue() );

            c.set( Calendar.MILLISECOND, 0 );
            c.set( Calendar.SECOND, 0 );
            c.set( Calendar.MINUTE, c1.get( Calendar.MINUTE ) );
            c.set( Calendar.HOUR_OF_DAY, c1.get( Calendar.HOUR_OF_DAY ) );

            return c.getTime();
        }
        
        public void setDate( Date date )
        {
            this.time.setValue( date );
            this.date.setDate( date );
        }
        
        private void initComponents()
        {
            JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(time, "HH:mm");
            time.setEditor(timeEditor);
            
            time.setValue(new Date());
            date.setDate(new Date());
            
            setLayout( new GridBagLayout() );
            
            add( date, 
                 new GridBagConstraints( 0, 0, 1, 1, 1.0, 0, 
                                         GridBagConstraints.CENTER, 
                                         GridBagConstraints.HORIZONTAL, 
                                         new Insets( 5, 5, 5, 5 ), 0, 0 ) );
            
            add( time, 
                 new GridBagConstraints( 1, 0, 1, 1, 1.0, 0, 
                                         GridBagConstraints.CENTER, 
                                         GridBagConstraints.HORIZONTAL, 
                                         new Insets( 5, 5, 5, 5 ), 0, 0 ) );
            
            time.addChangeListener( new ChangeListener()
            {
                @Override
                public void stateChanged( ChangeEvent e )
                {
                    Calendar c = Calendar.getInstance();
                    c.setTime( date.getDate() );
                    
                    Calendar c1 = Calendar.getInstance();
                    c1.setTime( (Date) time.getValue() );
                    
                    c.set( Calendar.MILLISECOND, 0 );
                    c.set( Calendar.SECOND, 0 );
                    c.set( Calendar.MINUTE, c1.get( Calendar.MINUTE ) );
                    c.set( Calendar.HOUR_OF_DAY, c1.get( Calendar.HOUR_OF_DAY ) );
                    
                    date.setDate( c.getTime() );
                }
            });
            
            date.getEditor().setEditable( false );
        }
        
        private JXDatePicker date = new JXDatePicker();
        private JSpinner time = new JSpinner( new SpinnerDateModel() );
    }
    
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private DeviceInfoTable deviceInfoTable;
    private JButton btRefresh = new JButton("ok");
    private DateTimePicker firstDateTime = new DateTimePicker();
    private DateTimePicker lastDateTime = new DateTimePicker();
}
